package com.atguigu.aclservice.service.impl;

import com.atguigu.aclservice.entity.Permission;
import com.atguigu.aclservice.entity.RolePermission;
import com.atguigu.aclservice.mapper.RolePermissionMapper;
import com.atguigu.aclservice.service.PermissionService;
import com.atguigu.aclservice.service.RolePermissionService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色权限 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-01-12
 */
@Service
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionMapper, RolePermission> implements RolePermissionService {

    @Resource
    private PermissionService permissionService;

    @Override
    public void deleteListByRoleId(String id) {
        this.remove(new QueryWrapper<RolePermission>().eq("role_id", id));
    }

    @Override
    public void deleteListByRoleIds(List<String> idList) {
        idList.forEach((id) -> {
            this.remove(new QueryWrapper<RolePermission>().eq("role_id", id));
        });
    }

    @Override
    public List<Permission> getPermissionsByRoleIds(String roleId) {
        QueryWrapper<RolePermission> wrapper = new QueryWrapper<RolePermission>().eq("role_id", roleId).select("permission_id");
        List<RolePermission> rolePermissions = this.baseMapper.selectList(wrapper);
        List<String> collect = rolePermissions.stream().map((cds) -> {
            return cds.getPermissionId();
        }).collect(Collectors.toList());
        List<Permission> permissions = permissionService.getByIds(collect);

        return permissions;
    }
}
